package wangjg.mda.tra;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;

import wangjg.mda.model.db.Column;
import wangjg.mda.model.db.Table;
import wangjg.mda.model.obj.ManyToMany;
import wangjg.mda.model.obj.Method;
import wangjg.mda.model.obj.Model;
import wangjg.mda.model.obj.ModelField;
import wangjg.mda.model.obj.Parameter;
import wangjg.mda.model.obj.Reference;
import wangjg.mda.model.view.Form;
import wangjg.mda.model.view.Input;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

public class ModelTrans {

	public void saveModel(Object model, String filePath) {
		XStream xs = this.getXStream();
		FileOutputStream fs=null;
		try {
			fs = new FileOutputStream(filePath);
			OutputStreamWriter writer = new OutputStreamWriter(fs, "utf-8");
			xs.toXML(model, writer);
		} catch (IOException e1) {
			e1.printStackTrace();
		}finally{
			try{
				if(fs!=null)fs.close();
			}catch(Exception e){e.printStackTrace();}
		}
	}

	public void loadModel(Object model, String filePath) {
		XStream xs = this.getXStream();
		FileInputStream fis = null;
		try {
			fis = new FileInputStream(filePath);
			InputStreamReader isr = new InputStreamReader(fis, "utf-8");
			xs.fromXML(isr, model);
		} catch (IOException ex) {
			ex.printStackTrace();
		}finally{
			try{
				if(fis!=null)fis.close();
			}catch(Exception e){e.printStackTrace();}
		}
	}
	private XStream getXStream(){
		XStream xs = new XStream(new DomDriver());
//		XStream xs = new XStream();
		xs.alias("model", Model.class);
		xs.alias("field", ModelField.class);
		xs.alias("method", Method.class);
		xs.alias("parameter", Parameter.class);
		xs.alias("reference", Reference.class);
		xs.alias("ManyToMany", ManyToMany.class);
		return xs;
	}

	/* model 和 form互转 */
	public void modelToForm(Model model, Form form) {
		form.setName(model.getName());
		List<Input> items = new ArrayList<Input>();
		form.setItems(items);
		for (ModelField field : model.getFields()) {
			Input input = new Input();
			input.setName(field.getName());
			input.setLable(field.getName());
			input.setType(null);
			items.add(input);
		}
	}

	public void formToModel(Form form, Model model) {
		model.setName(form.getName());
		List<ModelField> fields = new ArrayList<ModelField>();
		model.setFields(fields);
		for (Input input : form.getItems()) {
			ModelField field = new ModelField();
			field.setName(input.getName());
			field.setType(null);
		}
	}

	/* model 和 db互转 */
	public void modelToDb(Model model, Table table) {
		table.setName(model.getName());
		List<Column> columns = new ArrayList<Column>();
		table.setColumns(columns);
		for (int i = 0; i < model.getFields().size(); i++) {
			ModelField field = model.getFields().get(i);
			Column column = new Column();
			column.setName(field.getName());
			column.setType(null);
			columns.add(column);
		}
	}

	public void dbToModel(Table table, Model model) {
		model.setName(table.getName());
		List<ModelField> fields = new ArrayList<ModelField>();
		model.setFields(fields);
		for (Column column : table.getColumns()) {
			ModelField field = new ModelField();
			field.setName(column.getName());
			field.setType(null);
		}
	}

	public static void main(String args[]) throws Exception {
		ModelTrans mu = new ModelTrans();
		Model model = new Model();
		List<ModelField> fields = new ArrayList<ModelField>();
		fields.add(new ModelField());
		ModelField f = new ModelField();
		f.setName("hello");
		f.setType("String");
		f.setNullable(false);
		Reference r = new Reference();
		r.setModelName("test");
		f.setReference(r);
		fields.add(f);
		Method mtd = new Method();
		mtd.setName("save");
		List<Parameter> parameters = new ArrayList<Parameter>();
		Parameter p = new Parameter();
		p.setName("pp");
		parameters.add(p);
		mtd.setParameters(parameters);
		List<Method> methods = new ArrayList<Method>();
		methods.add(mtd);
		model.setMethods(methods);
		model.setFields(fields);
		mu.saveModel(model, "E:/temp/m.xml");
		
		mu.loadModel(model, "E:/temp/m.xml");
		System.out.println(model);
	}

}
